import { createRouter, createWebHashHistory, createWebHistory } from 'vue-router'
// import home from '../Views/home.vue'
// import about from '../Views/about.vue'
// 分包处理 路由懒加载
// const home = () => import(/*webpackChunkName:'home'*/"../Views/home.vue")
const about = () => import(/*webpackChunkName:'about'*/"../Views/about.vue")
const router = createRouter({
    // 采用的模式
    // 1.哈希模式
    // history: createWebHashHistory(),
    // 2.history模式
    history: createWebHistory(),
    routes: [
        // 默认路径 重定向
        {
            path: "/",
            redirect: "/home"
        },
        {
            // name属性 独一无二
            name: "home",
            path: "/home",
            // 写到这里更方便
            component: () => import(/*webpackChunkName:'home'*/"../Views/home.vue"),
            // 自定义属性
            meta: {
                name: "小染",
                age: 19
            },
            // 路由嵌套
            children: [
                {
                    // 重定向
                    path: "/home",
                    redirect: "/home/HomeReconmand"
                },
                {
                    path: "HomeReconmand",
                    component: () => import("../Views/HomeRecomand.vue")
                },
                {
                    path: "HomeRank",
                    component: () => import("../Views/HomeRank.vue")
                }
            ]
        },
        {
            path: "/about",
            component: about
        },
        {
            path: "/user/:id",
            component: () => import("../Views/user.vue")
        },
        {
            path: "/:pathMatch(.*)",
            // 这个是数组形式
            // path: "/:pathMatch(.*)*",
            component: () => import("../Views/notfound.vue")
        },
        {
            path: "/login",
            component: () => import("../Views/login.vue")
        },
        {
            path: "/order",
            component: () => import("../Views/order.vue")
        }
    ]
})

let isadmin = true
if (isadmin) {
    router.addRoute({
        name: "admin",
        path: "/admin",
        component: () => import('../Views/admin.vue')
    })
    // 二级路由 " "传入的是name
    router.addRoute("admin", {
        path: "vip",
        component: () => import("../Views/adminvip.vue")
    })
}

// 路由导航守卫
// 1.进入任何一个页面之前都要跳转到登录页面
// router.beforeEach((to, from) => {
//     if (to.path !== "/login") {
//         return "/login"
//     }
// })
// 2.进入到订单页面，判断用户是否登陆
router.beforeEach((to,from)=> {
    const token = localStorage.getItem("token")
    if (to.path === "/order" && !token) {
        return '/login'
    }
})
export default router 